package cn.tx.sboot.controller;/*
 *
 * @author 陈粒
 * @version 创建时间：2021/8/12 15:19
 */

import cn.tx.sboot.pojo.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;

@RestController
@RequestMapping("stu")
public class StudentController {
    @RequestMapping("get")
    public Object get(Student st) {
        Connection conn = null;
        PreparedStatement pstm = null;
        try {
            //1.注册驱动，自动注册了
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studenttest?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8", "root", "root");
            //3.书写sql语句
            String sql = "insert into tb_student values(null,?,?,?,?,?,?)";
            //4.获取sql语句
            pstm = conn.prepareStatement(sql);
            //5.给占位符赋值
            pstm.setObject(1,st.getName());//给第一个问号赋值
            pstm.setObject(2,st.getAge());
            pstm.setObject(3,st.getSex());
            pstm.setObject(4, Arrays.toString(st.getHobby()));//把数组变成字符串
            pstm.setObject(5,st.getEducation());
            pstm.setObject(6,st.getDate());
            //6.执行sql
            int count = pstm.executeUpdate();
            System.out.println("执行成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return st;
    }

}
